package com.mzj.saas.mservice.smartlock.controller;

import com.mzj.saas.commons.GenerateNiceString;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.lockManager.entity.SmartLockBrand;
import com.mzj.saas.mservice.smartlock.service.yaluete.SmartLockYltService;
import com.mzj.saas.mservice.smartlock.vo.DynamicPasswordVO;
import com.mzj.saas.mservice.smartlock.vo.SmartLockVO;
import com.mzj.saas.mservice.smartlock.vo.yaluete.YltAuthorizationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 亚略特-测试智能锁接口
 */
@Controller
@RequestMapping("device/lock/ylt")
public class SmartLockYltController {

	@Autowired
	private SmartLockYltService smartLockYltService;
	
	/**
	 * 锁绑定商户
	 * 
	 * @param accessToken
	 * @param imei
	 * @param brandId
	 * @return
	 * @author shun
	 * @date 2021年6月28日
	 */
	@PostMapping("/bindimei")
	@ResponseBody
	public ResultBean<String> bindimei(@RequestHeader String accessToken, @RequestParam String imei,
			@RequestParam Long brandId) {
		return smartLockYltService.merchantinfoBindLock(imei, brandId);
	}
	
	/**
	 * 解绑商户
	 * 
	 * @param accessToken
	 * @param imei
	 * @param brandId
	 * @return
	 * @author shun
	 * @date 2021年6月28日
	 */
	@PostMapping("/merchantinfoUnBindLock")
	@ResponseBody
	public ResultBean<String> merchantinfoUnBindLock(@RequestParam String imei,
			@RequestParam Long brandId) {
		return smartLockYltService.merchantinfoUnBindLock(imei, brandId);
	}

	/**
	 * 开锁授权
	 * 
	 * @param accessToken
	 * @param vo
	 * @return
	 */
	@PostMapping("/operationPassword")
	@ResponseBody
	public ResultBean<String> operationPassword(@RequestHeader String accessToken, @RequestParam Long brandId) {
		YltAuthorizationVO vo = new YltAuthorizationVO();

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		vo.setfLockMAC("000000010704");// 锁 MAC 地 址
		vo.setfNumber("AL1000501");// 商户编号
		String userId = GenerateNiceString.generateNiceString();
		vo.setfAppUserID(userId);// 授权用户
																// ID（要求32位的16进制数）
		/*vo.setfBeginDate(format.format(new Date()));// 授权开始时间
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, +3);
		vo.setfEndDate(format.format(calendar.getTime()));// 授权结束时间*/

		vo.setfBeginDate("2021-09-30 10:43:59");
		vo.setfEndDate("2022-09-30 10:43:59");


		vo.setfOpenByPassword(0);// 是否可以密码开锁
		vo.setfOpenByIDCard(1);// 是否可以二代证开锁
		vo.setfOpenByBLE(0);// 是否可以蓝牙开锁
		vo.setfOpenByFinger(0);// 是否可以使用指纹开锁
		vo.setfOpenByICCard(0);// 是否可以使用 IC 卡开锁

		// vo.setfIDCardID("");// 身份证 ID（16位）
		 vo.setfIDCardNumber("44092319940114773X");// 身份证号
		 //vo.setfIDCardName("44092319940114773X");// 身份证姓名
		 //vo.setfICCardNumber("");// IC 卡号
		//vo.setfOpenPassword("123467");// 开锁密码
		// vo.setfFingerFeature("");// 指纹特征
		
		SmartLockVO svo = new SmartLockVO();
		svo.setYltAuth(vo);
		return smartLockYltService.lockAuthorization(brandId, svo);
	}
	
	/**
	 * 更新开锁授权指令
	 * 
	 * @param accessToken
	 * @param vo
	 * @return
	 */
	@PostMapping("/updateAuthorize")
	@ResponseBody
	public ResultBean<String> updateAuthorize(@RequestHeader String accessToken, @RequestParam Long brandId) {
		return smartLockYltService.updateAuthorize(null, brandId);
	}
	
	/**
	 * 取消开锁授权指令/清除所有授权
	 * 
	 * @param accessToken
	 * @param vo
	 * @return
	 */
	@PostMapping("/deauthorize")
	@ResponseBody
	public ResultBean<String> deauthorize(@RequestHeader String accessToken, @RequestParam String imei, @RequestParam Long brandId) {
		SmartLockVO svo = new SmartLockVO();
		svo.setSn(imei);
		svo.setfAuthorizationID("f5c79dca001043a79b47fa939b6311de");
		return smartLockYltService.deauthorize(svo, brandId, 1);
	}
	
	/**
	 * 动态密码-获取紧急密码
	 * 
	 * @param accessToken
	 * @param vo
	 * @return
	 */
	@PostMapping("/urgentPassword")
	@ResponseBody
	public ResultBean<List<DynamicPasswordVO>> getUrgentPassword(@RequestHeader String accessToken, @RequestParam String imei, @RequestParam Long brandId) {
		return smartLockYltService.getUrgentPassword(imei, brandId);
	}
	
	/**
     * 亚略特-token
     *
     * @param accessToken
     * @param vo
     * @return
     */
    @RequestMapping("/token/{mac}")
    @ResponseBody
    public ResultBean<String> getYltToken(@PathVariable String mac) {
    	SmartLockBrand brand = smartLockYltService.findLockBrandId(mac);
    	if(brand == null){
    		return ResultBean.failedResult("设备或品牌信息未找到");
    	}
        return ResultBean.successfulResult(smartLockYltService.getLoginToken(brand));
    }
    
    /**
     * 修改商户信息
     * @param fNumber
     * @param fPlatformUrl
     * @return
     * @author shun
     * @date 2022年6月30日
     */
	@PutMapping("/info")
	@ResponseBody
	public ResultBean<String> updateMerchantinfoList(@RequestParam String fNumber,
			@RequestParam String fPlatformUrl) {
		return smartLockYltService.updateMerchantinfoList(fNumber, fPlatformUrl);
	}
}
